var t = require("../../requests/newapi"), e = t.getVerifyCode, n = t.getEnquireRecord, o = getApp();

Page({
    data: {
        hasLogin: !0,
        showInput: !0,
        isLoading: !1,
        isError: !1,
        disabled: !0,
        tel: "",
        code: "",
        countdown: 0,
        records: []
    },
    onLoad: function(t) {},
    onReady: function() {},
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
    onShow: function() {
        var t = this, e = o.globalData.hasLogin;
        if (this.setData({
            showInput: !e,
            hasLogin: e,
            isLoading: e
        }), e) {
            var n = wx.getStorageSync("UserToken");
            wx.showLoading({
                title: "",
                mask: !0
            }), this.getRecord({
                method: "getdealerorder.bytoken",
                usertoken: n,
                t: Date.now()
            }).then(function(e) {
                wx.hideLoading(), t.setData({
                    records: e,
                    isLoading: !1
                });
            }).catch(function(e) {
                wx.hideLoading(), wx.showToast({
                    title: e.Message || "出错了",
                    icon: "none"
                }), t.setData({
                    isError: !0
                });
            });
        }
    },
    getRecord: function(t) {
        return new Promise(function(e, o) {
            n(t).then(function(t) {
                2 !== t.Status ? o(t) : (0 === t.Data.length && wx.showToast({
                    title: "暂无询价记录",
                    icon: "none",
                    duration: 3e3
                }), e(t.Data));
            }).catch(function(t) {
                o(t);
            });
        });
    },
    onHide: function() {},
    onUnload: function() {
        clearInterval(this.timer);
    },
    inputTel: function(t) {
        var e = t.detail.value;
        this.setData({
            tel: e,
            disabled: 11 !== e.length || 6 !== this.data.code.length
        });
    },
    clearTel: function() {
        this.setData({
            tel: "",
            disabled: !0
        });
    },
    inputCode: function(t) {
        var e = t.detail.value;
        this.setData({
            code: e,
            disabled: 6 !== e.length || 11 !== this.data.tel.length
        });
    },
    getCode: function() {
        var t = this, n = this.data, o = n.tel;
        if (!(n.countdown > 0)) {
            if (!this.isTelValid(o)) return wx.showToast({
                title: "手机号填写错误",
                icon: "none"
            });
            this.setData({
                countdown: 59
            }), this.timer = setInterval(function() {
                var e = t.data.countdown;
                0 === e ? clearInterval(t.timer) : t.setData({
                    countdown: e - 1
                });
            }, 1e3), wx.showLoading({
                mask: !0
            }), e({
                method: "getdealerorder.sendmsg",
                phoneno: o,
                udid: wx.getStorageSync("opid"),
                t: Date.now()
            }).then(function(t) {
                wx.showToast({
                    title: t.Message,
                    icon: "none"
                });
            }).catch(function() {
                wx.showToast({
                    title: "发送失败，请重试",
                    icon: "none"
                });
            });
        }
    },
    searchRecord: function() {
        var t = this, e = this.data, n = e.disabled, o = e.tel, i = e.code;
        if (!n) {
            if (!this.isTelValid(o)) return wx.showToast({
                title: "手机号填写错误",
                icon: "none"
            });
            wx.showLoading({
                title: "",
                mask: !0
            }), this.getRecord({
                method: "getdealerorder.bymobile",
                phoneno: o,
                vcode: i,
                t: Date.now()
            }).then(function(e) {
                wx.hideLoading(), t.setData({
                    showInput: !1,
                    isLoading: !1,
                    records: e
                });
            }).catch(function(t) {
                wx.hideLoading(), wx.showToast({
                    title: t.Message,
                    icon: "none"
                });
            });
        }
    },
    changeTel: function() {
        this.setData({
            showInput: !0
        });
    },
    isTelValid: function(t) {
        return /^1[34578]\d{9}$/.test(t);
    }
});